import BaseXform from '../base-xform';
import ListXform from '../list-xform';

import CustomFilterXform from './custom-filter-xform';
import FilterXform from './filter-xform';

class FilterColumnXform extends BaseXform {
    constructor() {
        super();

        this.map = {
            customFilters: new ListXform({
                tag: 'customFilters',
                count: false,
                empty: true,
                childXform: new CustomFilterXform(),
            }),
            filters: new ListXform({
                tag: 'filters',
                count: false,
                empty: true,
                childXform: new FilterXform(),
            }),
        };
    }

    get tag() {
        return 'filterColumn';
    }

    prepare(model, options) {
        model.colId = options.index.toString();
    }

    render(xmlStream, model) {
        if (model.customFilters) {
            xmlStream.openNode(this.tag, {
                colId: model.colId,
                hiddenButton: model.filterButton ? '0' : '1',
            });

            this.map.customFilters.render(xmlStream, model.customFilters);

            xmlStream.closeNode();
            return true;
        }
        xmlStream.leafNode(this.tag, {
            colId: model.colId,
            hiddenButton: model.filterButton ? '0' : '1',
        });
        return true;
    }

    parseXmlObject(node) {
        if (node._name === this.tag) {
            this.model = {
                filterButton: node._attributes.hiddenButton === '0',
            };
            node._elements?.forEach(rel => {
                this.parser = this.map[rel._name];
                if (this.parser) {
                    this.parser.parseXmlObject(rel);
                    this.parser = undefined;
                }
            })
            this.model.customFilters = this.map.customFilters.model;
            return true
        }
        return false
    }

    parseOpen(node) {
        if (this.parser) {
            this.parser.parseOpen(node);
            return true;
        }
        const { attributes } = node;
        switch (node.name) {
            case this.tag:
                this.model = {
                    filterButton: attributes.hiddenButton === '0',
                };
                return true;
            default:
                this.parser = this.map[node.name];
                if (this.parser) {
                    this.parseOpen(node);
                    return true;
                }
                throw new Error(`Unexpected xml node in parseOpen: ${JSON.stringify(node)}`);
        }
    }

    parseText() {
    }

    parseClose(name) {
        if (this.parser) {
            if (!this.parser.parseClose(name)) {
                this.parser = undefined;
            }
            return true;
        }
        switch (name) {
            case this.tag:
                this.model.customFilters = this.map.customFilters.model;
                return false;
            default:
            // could be some unrecognised tags
                return true;
        }
    }
}

export default FilterColumnXform;
